﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using CommomFunctions;

namespace Árvore_Binária_de_Pesquisa
{
    public class Controls : CommomFunctions.Controls
    {
        /// <summary>
        /// firstTime -> indicates if it is the first time that the function gridSizeChanged is being executed
        /// txtLetters -> the textBox where the user inserts the information about the node s/he wants to operate on
        /// elemLables -> each element in the tree is represented by a label with colored background and border
        /// myArrows -> the arrows that represent the tree edges
        /// myAnimations -> the objects and functions that will be used to animate the operations
        /// myTree -> the objects, structures and functions that represent the binary search tree
        /// MenuItems -> the MenuItems that will be enabled or disabled according to the operation
        /// </summary>
        private bool firstTime = true;
        protected static TextBox txtInput = new TextBox();
        protected Labels elemLabels = new Labels();        
        protected Arrows myArrows = new Arrows();
        protected static Animations myAnimations = new Animations();
        protected TreeStructure myTree = new TreeStructure();
        protected MenuItem[] MenuItems;
        
        /// <summary>
        /// Adds to "gridi" the controls that were not declared/created in the xaml file.
        /// </summary>
        public void addOtherControls (Grid gridi, TextBox txtEntry, ControlVariables cv)
        {
            gd = gridi;
            txtInput = txtEntry;
            myArrows.createArrows(gd);
            elemLabels.createLabels(gd);
            myAnimations.createAnimations(gd, cv);

            createTrace(5, 4, 6, 3, new Thickness(30, 10, 55, 10), 170, 400);
            createLogo(7, 2, new Thickness(0, 30, 5, 5));
            createTitle("Manipulação de Árvores Binárias de Pesquisa", 1, 7, new Thickness(25, 20, 0, 0));
        }        

        /// <summary>
        /// Updates the labels size when the grid size changes.
        /// If it is the first time that this function is called then the function "initiateMargins"
        /// will be called and the margins of the arrows will be set.
        /// </summary>
        /// <param name="cv"></param>
        public void gridSizeChanged(ControlVariables cv)
        {
            elemLabels.gridSizeChanged();
            
            if (firstTime)
            {
                firstTime = false;

                cv.initiateMargins();
            }
        }

        /// <summary>
        /// Calls the respective button function
        /// </summary>
        /// <param name="bc"></param>
        public void buttonClicked(ButtonClicked bc, TextBox txtInput, ControlVariables cv)
        {
            switch (bc)
            {
                case ButtonClicked.Insert: Buttons.btnInsere_Click(cv);
                    break;
                case ButtonClicked.Search: Buttons.btnProcura_Click(cv);
                    break;
                case ButtonClicked.Remove: Buttons.btnRemove_Click(cv);
                    break;
            }
        }

        public void buttonClicked(ButtonClicked bc, ControlVariables cv)
        {
            switch (bc)
            {
                case ButtonClicked.Create: Buttons.btnCreate_Click(btns, cv);
                    break;
                case ButtonClicked.Destroy: Buttons.btnDestroy_Click(btns, cv);
                    break;
                case ButtonClicked.Complete: Buttons.btnExhibitComplete_Click(btns, cv);
                    break;
            }
        }

        /// <summary>
        /// Calls the respective menu function
        /// </summary>
        /// <param name="bc"></param>
        public void menuClicked(TraversalClicked cc, ControlVariables cv)
        {
            if (cv.treeIsEmpty())
                return;

            Menus.visitNodes(cc, cv);
        }
                
        public void menuClicked(MenuClicked mc, MenuItem pred, MenuItem suc)
        {
            switch (mc)
            {
                case MenuClicked.removePredecessor: Menus.removePredecessor_Click(pred, suc);
                    break;
                case MenuClicked.removeSuccessor: Menus.removeSuccessor_Click(pred, suc);
                    break;
            }
        }
        
        public void menusToEnableDisable(MenuItem[] menus)
        {
            MenuItems = menus;
        }

    }
}
